package com.moxiao.yfjh.module.blind.service.commonphrases;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.moxiao.yfjh.module.blind.controller.admin.commonphrases.vo.CommonPhrasesCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.commonphrases.vo.CommonPhrasesExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.commonphrases.vo.CommonPhrasesPageReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.commonphrases.vo.CommonPhrasesUpdateReqVO;
import com.moxiao.yfjh.module.blind.convert.commonphrases.CommonPhrasesConvert;
import com.moxiao.yfjh.module.blind.dal.dataobject.commonphrases.CommonPhrasesDO;
import com.moxiao.yfjh.module.blind.dal.mysql.commonphrases.CommonPhrasesMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.moxiao.yfjh.module.blind.controller.admin.commonphrases.vo.*;
import com.moxiao.yfjh.framework.common.pojo.PageResult;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.blind.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 相亲会员常用语 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class CommonPhrasesServiceImpl implements CommonPhrasesService {

    @Resource
    private CommonPhrasesMapper commonPhrasesMapper;

    @Override
    public Long createCommonPhrases(CommonPhrasesCreateReqVO createReqVO) {
        // 插入
        CommonPhrasesDO commonPhrases = CommonPhrasesConvert.INSTANCE.convert(createReqVO);
        commonPhrasesMapper.insert(commonPhrases);
        // 返回
        return commonPhrases.getId();
    }

    @Override
    public void updateCommonPhrases(CommonPhrasesUpdateReqVO updateReqVO) {
        // 校验存在
        validateCommonPhrasesExists(updateReqVO.getId());
        // 更新
        CommonPhrasesDO updateObj = CommonPhrasesConvert.INSTANCE.convert(updateReqVO);
        commonPhrasesMapper.updateById(updateObj);
    }

    @Override
    public void deleteCommonPhrases(Long id) {
        // 校验存在
        validateCommonPhrasesExists(id);
        // 删除
        commonPhrasesMapper.deleteById(id);
    }

    private void validateCommonPhrasesExists(Long id) {
        if (commonPhrasesMapper.selectById(id) == null) {
            throw exception(COMMON_PHRASES_NOT_EXISTS);
        }
    }

    @Override
    public CommonPhrasesDO getCommonPhrases(Long id) {
        return commonPhrasesMapper.selectById(id);
    }

    @Override
    public List<CommonPhrasesDO> getCommonPhrasesList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return commonPhrasesMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<CommonPhrasesDO> getCommonPhrasesPage(CommonPhrasesPageReqVO pageReqVO) {
        return commonPhrasesMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CommonPhrasesDO> getCommonPhrasesList(CommonPhrasesExportReqVO exportReqVO) {
        return commonPhrasesMapper.selectList(exportReqVO);
    }

    @Override
    public CommonPhrasesDO getCommonPhrasesTodayLast(Date todayStart, Date todayEnd) {
        LambdaQueryWrapper<CommonPhrasesDO> lqw = Wrappers.lambdaQuery();
        if (Objects.nonNull(todayStart) && Objects.nonNull(todayEnd)) {
            lqw.between(CommonPhrasesDO::getCreateTime, todayStart, todayEnd);
        }
        lqw.orderByDesc(CommonPhrasesDO::getCreateTime);
        lqw.last("LIMIT " + 1);
        return commonPhrasesMapper.selectOne(lqw);
    }

    @Override
    public Integer getCommonPhrasesNo(String commonPhrasesNo) {
        return commonPhrasesMapper.getCommonPhrasesNo(commonPhrasesNo);
    }

}
